package gcontent.config.node.content;

import gcontent.config.error.ConfigEx;
import gcontent.config.node.GNode;
import gcontent.config.node.filter.FilterRefNode;
import gcontent.record.Record;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ContentNode extends GNode {
	
	private String type = "";
	private int record_type = -1;


	public String getType()
	{
		return this.type;
	}

	public int getRecordType()
	{
		return this.record_type;
	}


	public ContentNode(Node node) throws ConfigEx
	{
		NodeList node_list;	
		String record_str;
		
		this.type = node.getAttributes().getNamedItem("type").getNodeValue();
		if(this.type == null || this.type.length() == 0)
			throw new ConfigEx("ContentNode => type not defined");
		
		record_str = node.getAttributes().getNamedItem("record_type").getNodeValue();
		/* if(record_str == null || record_str.length() == 0)
			this.record_type = Record.RECORD_TYPE_REF; 
		
		if(record_str.equals("references")) 
			this.record_type = Record.RECORD_TYPE_REF;
		else if(record_str.equals("content"))
			this.record_type = Record.RECORD_TYPE_CONTENT;
		else
			throw new ConfigEx("unknown record_type value " + record_str);
			*/

		node_list = node.getChildNodes();
		for(int i = 0; node_list.item(i) != null; i++) {
			Node node_c;
			String name;
			
			node_c = node_list.item(i);
			name = node_c.getNodeName();
			if(name.equals("filters")) {
				NodeList refs = node_c.getChildNodes();
				for(int o = 0; refs.item(o) != null; o++) {
					Node node_r;		
					String name_r;
					node_r = refs.item(o);
					name_r = node_r.getNodeName();
					if(name_r.equals("ref")) {
						FilterRefNode filter_ref = new FilterRefNode(refs.item(o));
						if(filter_ref != null)
							filter.add(filter_ref);
					} else if(name_r.charAt(0) != '#')
						throw new ConfigEx("ContentNode : invalid node " + name);
				}
			} else if(name.charAt(0) != '#')
				throw new ConfigEx("ContentNode => unknown node " + name);
		}		
	}
}
